package com.example.wexcle.demos.web.service.impl;

import com.alibaba.excel.EasyExcel;
import com.example.wexcle.demos.web.entity.File;
import com.example.wexcle.demos.web.service.EasyExcelService;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@AllArgsConstructor
public class EasyExcelServiceImpl implements EasyExcelService {

    private final ApplicationContext applicationContext;
    /**
     * excle文件导入实现
     * @param file
     */
    @Override
    public void easyExcelImport(MultipartFile file) {
        try {
            long beginTime = System.currentTimeMillis();
            //加载文件读取监听器
            EasyExcelImportHandler listener = applicationContext.getBean(EasyExcelImportHandler.class);
            //easyexcel的read方法进行数据读取
            List<File> list = EasyExcel.read(file.getInputStream(), File.class, listener).sheet().doReadSync();
            System.out.println("导出文件耗时："+(System.currentTimeMillis() - beginTime)/1000+"--------------------");
           //导入数据没问题保存上传的文件到oss

        } catch (IOException e) {
            System.out.println("导入异常"+e.getMessage()+ e);
        }

    }
}
